package org.ilead.iserve.web;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.ilead.iserve.constants.LopConstants;
import org.ilead.iserve.model.News;
import org.ilead.iserve.model.Resource;
import org.ilead.iserve.service.INewsService;
import org.ilead.iserve.service.IResourceService;
import org.ilead.iserve.util.UrlEncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3006688333948078147L;
	private static final Logger log = LoggerFactory
			.getLogger(HomeAction.class);
	private List<Resource> resourceList = new ArrayList<Resource>();// ���ڲ�ѯ
	private IResourceService resourceService = null;
	private INewsService newsService = null;
	
	
	
	private int type = LopConstants.RESOURCE_TYPE_RECORD; //Ĭ����¼��
	
	private Resource resource = null;
	
	private List<News> newsList  = new ArrayList<News>();
	
	public String listShortResource() throws Exception {
		try {
			
			List<Resource> list = resourceService.findByType(type);
			
			if(list!= null){  
				if( list.size() <=5)
					resourceList = list;	
				else 
					resourceList = list.subList(0, 10);
			}
			
			//暂时不做加密
			if(resourceList!= null && resourceList.size() > 0) {
				resource = resourceList.get(0);
//				for (Resource r : resourceList)
//					UrlEncodeUtil.encode(r.getUrl());
			}
			
		} catch (Exception e) {
			log.error("No RESOURCE!", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * �������µ�������Ϣ
	 * @return
	 * @throws Exception
	 */
	public String listShortNews() throws Exception {
		try {
			List<News> list = newsService.findByCategroy(LopConstants.NEWS_TYPE_SHORTMESSAGE);
			
			if(list!= null){  
				if( list.size() <=8)
					newsList = list;	
				else 
					newsList = list.subList(0, 8);
			}
		} catch (Exception e) {
			log.error("No RESOURCE!", e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String showLopHome() throws Exception {
		return SUCCESS;
	}
	
	
	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public int getType() {
		return type;
	}


	public void setType(int type) {
		this.type = type;
	}


	
	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}

	public void setResourceService(IResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public List<Resource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(List<Resource> resourceList) {
		this.resourceList = resourceList;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}
	
	
}
